package com.example.oss;

import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.*;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.net.URL;
import java.util.Date;

/**
 * 实现文件上传的方法
 */
public class Demo {
    // Endpoint以华北2（北京）为例
    // OSS Endpoint
    private static final String endpoint = "https://oss-cn-beijing.aliyuncs.com";

    // 从环境变量或配置文件中获取AccessKey ID和AccessKey Secret
    private static final String accessKeyId = "LTAI5tN55tgyw956bJzemEZ6";
    private static final String accessKeySecret ="riKjI22au4JMf8R70QrxCfQDKdnGmk";
    // 阿里云账号的AccessKey ID和AccessKey Secret
    /**
     * 用户登录名称 ethereal@1124204521501608.onaliyun.com
     * 登录密码 fuyihang110120
     * AccessKey ID LTAI5tN55tgyw956bJzemEZ6
     * AccessKey Secret riKjI22au4JMf8R70QrxCfQDKdnGmk
     */

    public static void main(String[] args) throws Exception {
        // 填写Bucket名称，ethereal。
        String bucketName = "ethereal";
        // 填写Object完整路径，完整路径中不能包含Bucket名称，例如images/photo.jpg。
        //        String objectName = "images/test.png";
        //        String objectName = "video/test.mp4";
        String objectName = "music/test.mp3";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

        try {
            // 准备本地文件。
            File file = new File("D:\\视频制作\\情非得已.mp3");

            // 创建PutObjectRequest对象。
            PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, objectName, file);

            //生成预签名
            Date expiration = new Date(System.currentTimeMillis()+3600*1000*100);
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName,objectName);
            request.setExpiration(expiration);
            //生成预签名 url
            URL url = ossClient.generatePresignedUrl(request);
            //打印
            System.out.println("预签名："+url.toString());

            // 上传文件。
            PutObjectResult result = ossClient.putObject(putObjectRequest);

            // 打印上传结果。
            System.out.println("ETag: " + result.getETag());
            System.out.println("预签名："+url);
            System.out.println("预签名："+url.toString());
            System.out.println("VersionId: " + result.getVersionId());
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException:");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException:");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }

}